using System;

namespace ScenarioFramework.WorkflowEngine.DataModel.WorkflowProcesses
{
	public class XMLLog : bOSdotNET.XML.Data.XMLObjectSet
	{
		public const string MAIN_NODE = "Log";

		private const string OWNER_ATTRIBUTE	= "Owner";
		private const string RESULT_ATTRIBUTE	= "Result";
		private const string STATUS_ATTRIBUTE	= "Status";
		private const string DATE_ATTRIBUTE		= "Date";

		public const string CREATED_STATUS		= "CREATED";
		public const string QUEUED_STATUS		= "QUEUED";
		public const string RUNNING_STATUS		= "RUNNING";
		public const string COMPLETED_STATUS	= "COMPLETED";
		public const string ERROR_STATUS		= "ERROR";
		public const string MANUAL_STATUS		= "MANUAL";
		public const string WAITINGFOR_STATUS	="WAITINGEVENT";

		private string sOwner;
		private string sResult;
		private string sStatus;
		private string sDate;

		public XMLLog()
		{
			this.Init();
		}

		public XMLLog(System.Xml.XmlNode node)
		{
			Parse(node);
		}

		public string Owner 
		{
			get { return sOwner; }
			set { sOwner = value; }
		}

		public string Result 
		{
			get { return sResult; }
			set { sResult = value; }
		}

		public string Status 
		{
			get { return sStatus; }
			set { sStatus = value; }
		}

		public string Date 
		{
			get { return sDate; }
			set { sDate = value; }
		}

		override public void Init()
		{
			sOwner = string.Empty;
			sResult= string.Empty;
			sStatus= string.Empty;
			sDate= string.Empty;

			this.RemoveAll();
		}

		/** Parsering method to fill the object **/
		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			this.Init();

			System.Xml.XmlNode node;

			/** find the attributes situated on the root tag **/

			for ( int i=0; i<node2Parse.Attributes.Count; i++)
			{
				node= node2Parse.Attributes.Item(i);

				switch (node.Name)
				{
					case XMLLog.OWNER_ATTRIBUTE:
						sOwner = node.InnerText;
						break;
					case XMLLog.RESULT_ATTRIBUTE:
						sResult = node.InnerText;
						break;
					case XMLLog.STATUS_ATTRIBUTE:
						sStatus = node.InnerText;
						break;
					case XMLLog.DATE_ATTRIBUTE:
						sDate = node.InnerText;
						break;
				}
			}

			for ( int i=0; i<node2Parse.ChildNodes.Count;i++)
			{
				node = node2Parse.ChildNodes[i];

				switch ( node.Name) 
				{
					case XMLStatus.MAIN_NODE:
						this.Add ( new XMLStatus(node));
						break;
				}
			}
		}

		/**Method to retive a node from object **/
		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = new bOSdotNET.XML.XMLNode(XMLLog.MAIN_NODE);

			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLLog.OWNER_ATTRIBUTE, sOwner) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLLog.RESULT_ATTRIBUTE, sResult) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLLog.STATUS_ATTRIBUTE, sStatus) );
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLLog.DATE_ATTRIBUTE, sDate) );

			for ( int i=0; i<this.Count;i++)
			{
				xmlNode.AddNode ( this[i].GetXMLNode() );
			}
			
			return xmlNode;
		}

		public void AddStatus(string sDate, string sStatus)
		{
			XMLStatus status = new XMLStatus();
			status.Date = this.Date;
			status.Status = this.Status;

			this.Add ( status );

			this.Status = sStatus;
			this.Date  = sDate;
		}
	}
}